package com.ruoyi.crm.sale.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.crm.sale.domain.CrmCustomer;
import com.ruoyi.crm.sale.domain.CrmTeam;
import com.ruoyi.crm.sale.mapper.CrmCustomerMapper;
import com.ruoyi.crm.sale.service.ICrmCustomerService;
import com.ruoyi.crm.sale.service.ICrmTeamService;

/**
 * 客户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-07
 */
@Service
public class CrmCustomerServiceImpl implements ICrmCustomerService 
{
    @Autowired
    private CrmCustomerMapper crmCustomerMapper;
    @Autowired
    private ICrmTeamService crmTeamService;

    /**
     * 查询客户信息
     * 
     * @param id 客户信息ID
     * @return 客户信息
     */
    @Override
    public CrmCustomer selectCrmCustomerById(Long id)
    {
        return crmCustomerMapper.selectCrmCustomerById(id);
    }

    /**
     * 查询客户信息列表
     * 
     * @param crmCustomer 客户信息
     * @return 客户信息
     */
    @Override
    @DataScope(deptAlias = "t", userAlias = "b")
    public List<CrmCustomer> selectCrmCustomerList(CrmCustomer crmCustomer)
    {
        return crmCustomerMapper.selectCrmCustomerList(crmCustomer);
    }

    /**
     * 查询公海客户信息列表
     * 
     * @param crmCustomer 客户信息
     * @return 客户信息
     */
    @Override
    public List<CrmCustomer> selHighSeasCrmCustomerList(CrmCustomer crmCustomer)
    {
        return crmCustomerMapper.selectCrmCustomerList(crmCustomer);
    }

    /**
     * 新增客户信息
     * 
     * @param crmCustomer 客户信息
     * @return 结果
     */
    @Override
    public int insertCrmCustomer(CrmCustomer crmCustomer)
    {
    	crmCustomer.setCreateTime(DateUtils.getNowDate());
    	crmCustomer.setUpdateTime(DateUtils.getNowDate());
    	crmCustomer.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
    	
    	int id = crmCustomerMapper.insertCrmCustomer(crmCustomer);
    	
    	if(null != crmCustomer.getLeader() && crmCustomer.getLeader() != 0) {
    		CrmTeam crmTeam = new CrmTeam();
    		crmTeam.setUserId(crmCustomer.getLeader());
    		crmTeam.setBusinessId(crmCustomer.getId());
    		crmTeam.setIsLeader((long)1);//负责人
    		crmTeam.setTeamType((long)1);//团队类型 1：客户  2：商机
    		crmTeamService.setLeader(crmTeam);
    	}
    	
    	
        return id;
    }

    /**
     * 修改客户信息
     * 
     * @param crmCustomer 客户信息
     * @return 结果
     */
    @Override
    public int updateCrmCustomer(CrmCustomer crmCustomer)
    {
    	crmCustomer.setUpdateTime(DateUtils.getNowDate());
    	crmCustomer.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
    	
    	if(null != crmCustomer.getLeader() && crmCustomer.getLeader() != 0) {
    		CrmTeam crmTeam = new CrmTeam();
    		crmTeam.setUserId(crmCustomer.getLeader());
    		crmTeam.setBusinessId(crmCustomer.getId());
    		crmTeam.setIsLeader((long)1);//负责人
    		crmTeam.setTeamType((long)1);//团队类型 1：客户  2：商机
    		crmTeamService.setLeader(crmTeam);
    	}
    	
        return crmCustomerMapper.updateCrmCustomer(crmCustomer);
    }

    /**
     * 批量删除客户信息
     * 
     * @param ids 需要删除的客户信息ID
     * @return 结果
     */
    @Override
    public int deleteCrmCustomerByIds(Long[] ids)
    {
        return crmCustomerMapper.deleteCrmCustomerByIds(ids);
    }

    /**
     * 删除客户信息信息
     * 
     * @param id 客户信息ID
     * @return 结果
     */
    @Override
    public int deleteCrmCustomerById(Long id)
    {
        return crmCustomerMapper.deleteCrmCustomerById(id);
    }

    /**
     * 查询当前客户数量
     * 
     * @return 结果
     */
    public int selCustomerCount(Map<String, Object> params) {
    	return crmCustomerMapper.selCustomerCount(params);
    }

    /**
     *客户来源分析
     * 
     * @return 结果
     */
    public List<Map<String, Object>> selCustomerSourceCount(Map<String, Object> params) {
    	return crmCustomerMapper.selCustomerSourceCount(params);
    }
}
